package leetcode2;

/**
 * 2. 两数相加
 * 给你两个 非空 的链表，表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的，并且每个节点只能存储 一位 数字。
 * 请你将两个数相加，并以相同形式返回一个表示和的链表。
 * 你可以假设除了数字 0 之外，这两个数都不会以 0 开头。
 * 输入：l1 = [2,4,3], l2 = [5,6,4]
 * 输出：[7,0,8]
 * 解释：342 + 465 = 807.
 */
class Solution {
    public static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }

        @Override
        public String toString() {
            return "ListNode{" +
                    "val=" + val +
                    ", next=" + next +
                    '}';
        }
    }
    public static void main(String[] args) {
        ListNode n1 = new ListNode(3);
        ListNode n2 = new ListNode(4, n1);
        ListNode n3 = new ListNode(2, n2);

        ListNode d1 = new ListNode(4);
        ListNode d2 = new ListNode(6, d1);
        ListNode d3 = new ListNode(5, d2);
        ListNode listNode = new Solution().addTwoNumbers2(n3, d3);
        System.out.println("listNode = " + listNode);

        int[] nums = {3,3};
        int target = 6;
//        int[] twoSum = new Solution().twoSum(nums, target);
//        System.out.println("twoSum = " + Arrays.toString(twoSum));
//        int[] twoSum2 = new Solution().twoSum2(nums, target);
//        System.out.println("twoSum2 = " + Arrays.toString(twoSum2));


    }
    // Iteration
    // @爱学习的饲养员
    // N is the size of l1, M is the size of l2
    // Time Complexity: O(max(M,N))
    // Space Complexity: O(max(M,N)) if dummy is counted else O(1)
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int next1 = 0; // 十位数
        int total = 0; // 两数相加之和
        ListNode dummy = new ListNode();
        ListNode cur = dummy;
        while (l1 != null && l2 != null) {
            total = l1.val + l2.val + next1;
            cur.next = new ListNode(total % 10);
            next1 = total / 10;
            l1 = l1.next;
            l2 = l2.next;
            cur = cur.next;
        }

        while (l1 != null) {
            total = l1.val + next1;
            cur.next = new ListNode(total % 10);
            next1 = total / 10;
            l1 = l1.next;
            cur = cur.next;
        }

        while (l2 != null) {
            total = l2.val + next1;
            cur.next = new ListNode(total % 10);
            next1 = total / 10;
            l2 = l2.next;
            cur = cur.next;
        }

        if (next1 != 0) {
            cur.next = new ListNode(next1);
        }

        return dummy.next;
    }

    /** 递归
     * @param l1
     * @param l2
     * @return
     */
    public ListNode addTwoNumbers2(ListNode l1, ListNode l2) {
        int total = l1.val + l2.val;
        int next1 = total / 10;
        ListNode res = new ListNode(total%10);
        if (l1.next != null || l2.next != null || next1 != 0) {
            l1 = l1.next!=null ? l1.next : new ListNode(0);
            l2 = l2.next!=null ? l2.next : new ListNode(0);
            l1.val += next1;
            res.next = addTwoNumbers2(l1, l2);
        }
        return res;
    }
}